package com.shsh.safemanage.action;

import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.shsh.safemanage.dao.DaoCreator;
import com.shsh.safemanage.dao.ProblemDao;
import com.shsh.safemanage.model.Problem;
import com.shsh.safemanage.util.Constants;
import com.shsh.safemanage.util.Util;

public class BrowseProblemAction {
	public List<Problem> browseProblems(HttpServletRequest request) {
		String depart1IDStr = request.getParameter(Constants.REQUEST_PARAMETER_DEPARTMENT_ID);
		String equipIDStr = request.getParameter(Constants.REQUEST_PARAMETER_EQUIP_ID);
		String problemTitle = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_TITLE);
		String problemContent = request.getParameter(Constants.REQUEST_PARAMETER_PROBLEM_CONTENT);
		String dateStart = request.getParameter(Constants.REQUEST_PARAMETER_DATE_START);
		String dateEnd = request.getParameter(Constants.REQUEST_PARAMETER_DATE_END);
		
		Long depart1ID = null;
		try {
			depart1ID = new Long(depart1IDStr);
		} catch (Exception e) {
		}
		
		Long equipID = null;
		try {
			equipID = new Long(equipIDStr);
		} catch (Exception e) {
		}
		
		if (depart1ID == null && equipID == null && Util.isNullOrEmpty(problemTitle) 
				&& Util.isNullOrEmpty(problemContent) && Util.isNullOrEmpty(dateStart) && Util.isNullOrEmpty(dateEnd)) {
			Calendar now = Calendar.getInstance();
			dateEnd = Util.formatDate(now.getTime());
			now.add(Calendar.DATE, -7);
			dateStart = Util.formatDate(now.getTime());
		}
		
		ProblemDao problemDao = DaoCreator.createProblemDao();
		List<Problem> problems = problemDao.searchProblems(depart1ID, equipID, problemTitle, problemContent, dateStart, dateEnd);
		
		return problems;
	}
}
